CFLAGS += -Wall -Wextra -g -fPIC -I include

NAME = common
LIB_NAME_STATIC  = lib$(NAME).a
LIB_NAME_DYNAMIC = lib$(NAME).so

SOURCE  = . essl \
	arch/lima_t6xx arch/lima_t600 arch/lima_t650 \
	binary binary/raw

Y_SOURCE = $(patsubst %.y, %.c, $(foreach dir, $(SOURCE), $(wildcard $(dir)/*.y)))
Y_HEADER = $(patsubst %.y, %.h, $(foreach dir, $(SOURCE), $(wildcard $(dir)/*.y)))
L_SOURCE = $(patsubst %.l, %.c, $(foreach dir, $(SOURCE), $(wildcard $(dir)/*.l)))

Y_OBJECTS = $(patsubst %.y, %.o, $(foreach dir, $(SOURCE), $(wildcard $(dir)/*.y)))
L_OBJECTS = $(patsubst %.l, %.o, $(foreach dir, $(SOURCE), $(wildcard $(dir)/*.l)))
C_OBJECTS = $(patsubst %.c, %.o, $(foreach dir, $(SOURCE), $(wildcard $(dir)/*.c)))
OBJECTS = $(Y_OBJECTS) $(L_OBJECTS) $(C_OBJECTS)

all: $(LIB_NAME_STATIC) $(LIB_NAME_DYNAMIC)

static: $(LIB_NAME_STATIC)
dynamic: $(LIB_NAME_DYNAMIC)

clean:
	rm -f $(OBJECTS)
	rm -f $(LIB_NAME)
	rm -f $(Y_SOURCE) $(Y_HEADER)
	rm -f $(L_SOURCE)
	rm -f $(LIB_NAME_STATIC) $(LIB_NAME_DYNAMIC)
	rm -f essl/parser.output

$(Y_SOURCE): %.c: %.y
	bison -v -d -p $$(basename $$(dirname $@))_yy -o $@ $<

$(L_SOURCE): %.c: %.l $(Y_SOURCE)
	flex -o $@ $<

$(C_OBJECTS): %.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $<

$(LIB_NAME_DYNAMIC): $(OBJECTS)
	$(CC) -shared -g -o $@ $^

$(LIB_NAME_STATIC): $(OBJECTS)
	$(AR) rcs $@ $^
